package com.pcbly.web.components.taglib;

import java.io.Writer;

import org.apache.struts2.components.Component;
import org.apache.struts2.util.TextProviderHelper;

import com.opensymphony.xwork2.util.ValueStack;

public class Button extends Component {

	private String id;

	private String name;

	private String type;

	private String value;

	private String className;

	private String title;

	private String textAndIcon;

	private String onclick;

	private String onfocus;

	private String onblue;

	private String onmouseover;

	private String onmouseout;
	
	private String style;
	
	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	private void setStack() {
		/** **** read stack start **** */
		getStyleStack();
		if (style == null) {
			style = "width: 75px;";
		}
		getIdStack();
		getNameStack();
		getTypeStack();
		getValue1Stack();
		getClassNameStack();

		if (className == null) {
			className = "x-btn";
		}
		getTitleStack();
		getTextAndIconStack();
		if (textAndIcon == null) {
			textAndIcon = "";
		}
		if (textAndIcon.equals("text")) {
			textAndIcon = "-noicon ";
		} else if (textAndIcon.equals("icon")) {
			textAndIcon = "-icon ";
		} else {
			textAndIcon = "-text-icon ";
		}
		getOnclickStack();
		if (onclick == null) {
			onclick = "";
		}
		getOnfocusStack();
		if (onfocus == null) {
			onfocus = "";
		}
		getOnblueStack();
		if (onblue == null) {
			onblue = "";
		}
		getOnmouseoverStack();
		if (onmouseover == null) {
			onmouseover = "";
		}
		getOnmouseoutStack();
		if (onmouseout == null) {
			onmouseout = "";
		}
		/** **** read stack end **** */
	}

	public Button(ValueStack stack) {
		super(stack);
		// TODO Auto-generated constructor stub
	}

	public boolean start(Writer writer) {
		boolean result = super.start(writer);
		try {
			StringBuilder sb = new StringBuilder("");
			setStack();
			sb.append("<table ");
			sb.append(" class=\"");
			sb.append(className);
			sb.append(" ");
			sb.append(className);
			sb.append(textAndIcon);
			sb.append("\" ");
			writerClassOnclick(sb);
			writerClassOnfocus(sb);

			writerClassOnblue(sb);
			writerClassOnmouseout(sb);
			writerClassOnmouseover(sb);

			sb.append(" cellspacing=\"0\" style=\"");
			sb.append(style);
			sb.append("\">\r\n");
			sb.append("\t<tbody class=\"");
			sb.append(className);
			sb.append("-small ");
			sb.append(className);
			sb.append("-icon-small-left\">\r\n");

			sb.append("\t\t<tr>");
			sb.append("\r\n\t\t\t<td class=\"");
			sb.append(className);
			sb.append("-tl\">\r\n");
			sb.append("\t\t\t\t<i> </i>");
			sb.append("\r\n\t\t\t</td>");
			sb.append("\r\n\t\t\t<td class=\"");
			sb.append(className);
			sb.append("-tc\"/>\r\n");
			sb.append("\t\t\t<td class=\"");
			sb.append(className);
			sb.append("-tr\">\r\n");
			sb.append("\t\t\t\t<i> </i>");
			sb.append("\r\n\t\t\t</td>\r\n");
			sb.append("\t\t</tr>\r\n");

			sb.append("\t\t<tr>");
			sb.append("\r\n\t\t\t<td class=\"");
			sb.append(className);
			sb.append("-ml\">\r\n");
			sb.append("\t\t\t\t<i> </i>\r\n");
			sb.append("\t\t\t</td>");
			sb.append("\r\n\t\t\t<td class=\"");
			sb.append(className);
			sb.append("-mc\">\r\n");
			sb.append("\t\t\t\t<em class=\"\" unselectable=\"on\">\r\n");
			sb.append("\t\t\t\t\t<button ");
			sb.append(" type=\"");
			sb.append(type);
			sb.append("\" ");
			
			sb.append(" id=\"");
			sb.append(name);
			sb.append("\" ");
			
			sb.append(" name=\"");
			sb.append(name);
			sb.append("\" ");
			
			sb.append("  class=\"");
			sb.append(className);
			sb.append("-text\"  >");
			sb.append(value);
			sb.append("</button>\r\n");
			sb.append("\t\t\t\t</em>\r\n");
			sb.append("\t\t\t</td>\r\n");
			sb.append("\t\t\t<td class=\"");
			sb.append(className);
			sb.append("-mr\">\r\n");
			sb.append("\t\t\t\t<i> </i>");
			sb.append("\r\n\t\t\t</td>");
			sb.append("\r\n\t\t</tr>\r\n");

			sb.append("\t\t<tr>");
			sb.append("\r\n\t\t\t<td class=\"");
			sb.append(className);
			sb.append("-bl\">\r\n");
			sb.append("\t\t\t\t<i> </i>");
			sb.append("\r\n\t\t\t</td>");
			sb.append("\r\n\t\t\t<td class=\"");
			sb.append(className);
			sb.append("-bc\"/>\r\n");
			sb.append("\t\t\t<td class=\"");
			sb.append(className);
			sb.append("-br\">\r\n");
			sb.append("\t\t\t\t<i> </i>");
			sb.append("\r\n\t\t\t</td>");
			sb.append("\r\n\t\t</tr>\r\n");

			sb.append("\r\n\t\t</tbody>");
			sb.append("\r\n</table>\r\n");
			writer.write(sb.toString());
		} catch (Exception e) {

		}
		return result;
	}

	private void writerClassOnfocus(StringBuilder sb) {
		if(!"".equals(onfocus)){
		sb.append(" onFocus=\"javascript:");
//		sb.append("this.className='");
//		sb.append(className);
//		sb.append(textAndIcon);
//		sb.append(" ");
//		sb.append(className);
//		sb.append("-focus';");
		sb.append(onfocus);
		sb.append("\" ");
		}
	}

	private void writerClassOnblue(StringBuilder sb) {
		if(!"".equals(onblue)){
		sb.append(" onBlue=\"javascript:");
//		sb.append("this.className='");
//		sb.append(className);
//		sb.append(textAndIcon);
//		sb.append(" ");
//		sb.append(className);
//		sb.append("-blue';");
		sb.append(onblue);
		sb.append("\" ");
		}

	}

	private void writerClassOnmouseout(StringBuilder sb) {

		sb.append(" onMouseout=\"javascript:this.className='");
		sb.append(className);
		sb.append(" ");
		sb.append(className);
		sb.append(textAndIcon);
		sb.append("';");
		sb.append(onmouseout);
		sb.append("\" ");
	}

	private void writerClassOnmouseover(StringBuilder sb) {

		sb.append(" onMouseover=\"javascript:this.className='");
		sb.append(className);
		sb.append(" ");
		sb.append(className);
		sb.append(textAndIcon);
		sb.append(" ");
		sb.append(className);
		sb.append("-over';");
		sb.append(onmouseover);
		sb.append("\" ");
	}

	private void writerClassOnclick(StringBuilder sb) {
		sb.append(" onClick=\"javascript:");
		sb.append("this.className='");
		sb.append(className);
		sb.append(" ");
		sb.append(className);
		sb.append(textAndIcon);
		sb.append(" ");
		sb.append(className);
		sb.append("-focus ");
		sb.append(className);
		sb.append("-over ");
		sb.append(className);
		sb.append("-click';");
		sb.append(onclick);
		sb.append("\" ");

	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getOnclick() {
		return onclick;
	}

	public void setOnclick(String onclick) {
		this.onclick = onclick;
	}

	public String getOnmouseover() {
		return onmouseover;
	}

	public void setOnmouseover(String onmouseover) {
		this.onmouseover = onmouseover;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTextAndIcon() {
		return textAndIcon;
	}

	public void setTextAndIcon(String textAndIcon) {
		this.textAndIcon = textAndIcon;
	}

	public void setOnfocus(String onfocus) {
		this.onfocus = onfocus;
	}

	public String getOnfocus() {
		return onfocus;
	}

	public void setOnblue(String onblue) {
		this.onblue = onblue;
	}

	public String getOnblue() {
		return onblue;
	}

	private void getStyleStack() {
		if (style != null && style.startsWith("%{") && style.endsWith("}")) {
			style = style.substring(2, style.length() - 1);
		}
	}
	private void getIdStack() {
		if (id != null && id.startsWith("%{") && id.endsWith("}")) {
			id = id.substring(2, id.length() - 1);
		}
	}

	private void getNameStack() {
		if (name != null && name.startsWith("%{") && name.endsWith("}")) {
			name = name.substring(2, name.length() - 1);
		}
	}

	private void getTypeStack() {
		if (type != null && type.startsWith("%{") && type.endsWith("}")) {
			type = type.substring(2, type.length() - 1);
		}
	}

	private void getValue1Stack() {
		if (value != null && value.startsWith("%{") && value.endsWith("}")) {
			value = value.substring(2, value.length() - 1);
			value = String.valueOf(this.getStack().findValue(value));
		} else {
			String tvalue=value;
			value = String.valueOf(this.getStack().findValue(value));
			
			if(value==null||"".equals(value)){
				value=TextProviderHelper.getText(tvalue, tvalue, this.getStack());
			}
		}
	}

	private void getClassNameStack() {
		if (className != null && className.startsWith("%{")
				&& className.endsWith("}")) {
			className = className.substring(2, className.length() - 1);
		}
	}

	private void getTitleStack() {
		if (title != null && title.startsWith("%{") && title.endsWith("}")) {
			title = title.substring(2, title.length() - 1);
		}
	}

	private void getTextAndIconStack() {
		if (textAndIcon != null && textAndIcon.startsWith("%{")
				&& textAndIcon.endsWith("}")) {
			textAndIcon = textAndIcon.substring(2, textAndIcon.length() - 1);
		}
	}

	private void getOnclickStack() {
		if (onclick != null && onclick.startsWith("%{")
				&& onclick.endsWith("}")) {
			onclick = onclick.substring(2, onclick.length() - 1);
		}
	}

	private void getOnfocusStack() {
		if (onfocus != null && onfocus.startsWith("%{")
				&& onfocus.endsWith("}")) {
			onfocus = onfocus.substring(2, onfocus.length() - 1);
		}
	}

	private void getOnblueStack() {
		if (onblue != null && onblue.startsWith("%{") && onblue.endsWith("}")) {
			onblue = onblue.substring(2, onblue.length() - 1);
		}
	}

	private void getOnmouseoverStack() {
		if (onmouseover != null && onmouseover.startsWith("%{")
				&& onmouseover.endsWith("}")) {
			onmouseover = onmouseover.substring(2, onmouseover.length() - 1);
		}
	}

	private void getOnmouseoutStack() {
		if (onmouseout != null && onmouseout.startsWith("%{")
				&& onmouseout.endsWith("}")) {
			onmouseout = onmouseout.substring(2, onmouseout.length() - 1);
		}
	}

	public void setOnmouseout(String onmouseout) {
		this.onmouseout = onmouseout;
	}

	public String getOnmouseout() {
		return onmouseout;
	}
}
